package com.yangming.common.wxpay;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author PengShuiMing
 * @create 2018-09-11
 **/
@Configuration
public class WXConfig {
    @Autowired
    private WXProperties wxProperties;
    /**
     * 微信基础配置
     * @return
     */
    @Bean
    public WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage(){
        WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpInMemoryConfigStorage.setAppId(wxProperties.getAppId());
        wxMpInMemoryConfigStorage.setSecret(wxProperties.getSecret());
        return wxMpInMemoryConfigStorage;
    }

    /**
     * 微信核心service注入
     * @return
     */
    @Bean
    public WxMpServiceImpl wxMpService(WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage){
        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
        return wxMpService;
    }

    /**
     * 微信支付配置
     * @return
     */
    @Bean
    public WxPayConfig wxPayConfig(){
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(wxProperties.getAppId());
        wxPayConfig.setMchId(wxProperties.getMchId());
        wxPayConfig.setMchKey(wxProperties.getMchKey());
        wxPayConfig.setKeyPath(wxProperties.getKeyPath());
        return wxPayConfig;
    }

    /**
     * 微信支付service注入
     * @return
     */
    @Bean
    public WxPayServiceImpl wxPayService(WxPayConfig wxPayConfig){
        WxPayServiceImpl service = new WxPayServiceImpl();
        service.setConfig(wxPayConfig);
        return service;
    }

}
